1. /* snfagrup.cpp by K.Tsuru */
  2. // function ID = 1000 DRADIX
  3. #ifndef SN_H
  4. #include "sn.h"
  5. #endif
  6. /***************************************************************
  7. Comparing two values from the position of one, return how many digits
  8. agree each other.In the case of SDouble the exponent is not considerd
  9. and the head zeros are not counted.
  10. If an error ocuured return a negative value as follows.
  11. not DRADIX : -1
  12. different type : -2
  13. not in standard form : -3
  14. *****************************************************************/
  15. long AgreeUpTo(const SNumber& a, const SNumber& b){
  16. if( !a.Sign(1000) && !b.Sign(1000) ) return 1;
  17. if( (a.Radix() != DRADIX) || (b.Radix() != DRADIX) ) return -1;
  18. if( a.Type() != b.Type() ) return -2;
  19. if( (a.Type() == a.REAL) && (!a[1] || !b[1]) ) return -3;
  20. uint top = min(a.Head(), b.Head()) +1, j = 0;
  21. int at = 0, bt = 0, f, add = 0;
  22. long same;
  23. while( (j < top) && (a[j] == b[j]) ) j++;
  24. same = (long)j*DFIGURES;
  25. if(j < top){
  26. at = (int)a[j]; bt = (int)b[j];
  27. }
  28. if(a.Type() == a.REAL){
  29. add = -2*(int)DFIGURES; // 0.dddd
  30. if( j > 1 ) add += iFigures(a[1]); //a(1) == b(1), a[1]=0123, b[1]=0123, add+=3
  31. //It excludes five zeros in 0000.0, add=-5.
  32. if(at && bt){
  33. fType r = DRADIX/10;
  34. //agreeing digits in the last figure
  35. //at = 1230, bt = 1256 -> 1 == 1, 2 == 2, 3 != 5 : add += 2
  36. while(r && ( (at/r) == (bt/r) ) ){ r /= 10; add++; }
  37. }
  38. } else if(at && bt){
  39. //agreeing digits in the tail of top figure
  40. f = abs( at - bt ); // xx00 :number of zeros agree. 5634-1234=4400, add+=2
  41. while( f && !(f%10) ){ f /= 10; add++; }
  42. }
  43. same += add;
  44. return same > 0 ? same : 0;
  45. }

snfagrup.cpp : last modifiled at 2017/03/13 14:32:01(1,708 bytes)
created at 2016/04/11 11:36:47
The creation time of this html file is 2017/10/27 10:59:17 (Fri Oct 27 10:59:17 2017).